<template>
  <div class="container">
    <section class="f-ptp10">
      <div class='u-title'> 筛选查询</div>
      <div class='u-content u-filter f-ptp10'>
        <div class='u-filter-item'>
          <label for="">输入搜索：</label>
          <el-input class='f-w120' v-model="name" placeholder="用户名/姓名"></el-input>
        </div>
        <div class='u-filter-item'>
          <label for="">所属部门：</label>
          <SelectDepartment @getDepartment='getDepartment' class='f-w120' placeholder="请选择" />
          <!-- <el-select class='f-w120' v-model="permissionName" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select> -->
        </div>
        <el-button class='f-h40 f-mrt20 f-mbt10' @click="getList" type="primary">查询</el-button>
      </div>      
    </section>

    <div class='f-mtp20'>
      <div class='u-title'>
        <h2>组织架构</h2>
        <router-link class='u-title-btn' to='/home/addStaff'>添 加</router-link>
      </div>
      <el-table
        :data="staffList"
        border
        style="width: 100%">
        <el-table-column prop="username" label="成员账号" width="180"></el-table-column>
        <el-table-column prop="name" label="姓名" width="180"></el-table-column>
        <el-table-column prop="permissionName" label="所属部门" width="180"></el-table-column>
        <el-table-column prop="createTime" label="添加时间" width="180">
          <template slot-scope="scope">
            <span>{{format(scope.row.createTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="state" label="是否启用" width="180">
          <template slot-scope="scope">
            <el-switch
              @change="switchClickEvent(scope.row)"
              style="display: block"
              v-model="scope.row.state"
              active-value='normal'
              inactive-value='expired'
              active-color="#1ABC9C"
              inactive-color="#e4e4e4">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="manage" label="管理">
          <template slot-scope="scope">
            <router-link class='s-c-green f-csp f-mrt10' :to='"/home/editStaff/" + scope.row.id'>编 辑</router-link>
            <span @click="deleteBtnClickEvent(scope.row)" class='s-c-red f-csp'>删 除</span>
          </template>
        </el-table-column>
      </el-table>
      <div class='f-oh f-ptp20 f-pbt20'>
        <el-pagination
          class='f-fr'
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          background
          :total="totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import SelectDepartment from '../../components/SelectDepartment'
export default {
  name: 'OrganizationalStructureManage',
  components: {
    'SelectDepartment': SelectDepartment
  },
  data() {
    return {
      totalCount: 0,
      currentPage: 1,
      pageSize: 20,
      permissionName: '',
      name: '',
      staffList: []
    }
  },
  created: function() {
    this.getList();
  },
  methods: {
    format(date) {
      return this._utils.format(date, 'yyyy-mm-dd');
    },
    switchClickEvent(item) {
      console.log('item::', item);
      this.updateStaff({
        id: item.id,
        state: item.state
      }, '修改成功！');
    },
    // 删除按钮点击事件, 进行删除操作
    deleteBtnClickEvent(row) {
      this.$confirm('确定删除该成员！', '提示', {
        confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.updateStaff({
          id: row.id,
          state: 'deleted'
        }, '删除成功！');
      })
    },
    updateStaff(params, msg) {
      this.$store.dispatch('HANDLE_STAFF', params).then(res => {
        console.log('res:::', res);
        if(res.data.retCode == 0) {
          this.$message.success(msg);
          this.getList();
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      })
    },
    getDepartment(d) {
      console.log('d:::', d);
      this.permissionName = d.name;
    },
    // 获取员工列表
    getList() {
      const params = {
        pageNum: this.currentPage - 1,
        pageSize: this.pageSize,
        name: String(this.name),
        permissionName: this.permissionName
      }
      this.$store.dispatch('GET_STAFF_LIST', params).then(res => {
        if(res.data.retCode == 0) {
          this.staffList = res.data.data;
          this.totalCount = res.data.totalCount;
        } else {
          res.data.errorMsg && this.$message.warning(res.data.errorMsg);
        }
      })
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getList();
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
